package com.tuanzi.loan.web.bean.collaborate;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.collaborate.NewCollaborateChannelUser;
import com.tuanzi.loan.business.enums.CollaborateColShowEnum;
import com.tuanzi.loan.business.service.collaborate.CollaborateChannelUserService;
import com.tuanzi.loan.vo.request.Collaborate.CollaborateConfigListRequest;
import com.tuanzi.loan.vo.response.collaborate.CollaborateConfigListResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.collaborate.CollaborateConfigListLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.Objects;

/**
 * User: liangZongXi
 * Create time: 10:44 2018/9/18
 * Description:
 */

@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class CollaborateConfigListBean extends JSFListingBean<CollaborateConfigListRequest, CollaborateConfigListResponse>{

    private static final long serialVersionUID = 3241280987319303215L;

    private final CollaborateChannelUserService collaborateChannelUserService;

    @Autowired
    public CollaborateConfigListBean(CollaborateChannelUserService collaborateChannelUserService) {
        this.collaborateChannelUserService = collaborateChannelUserService;
    }

    @PostConstruct
    public void init(){
        initLazyDataModel();
    }

    private void initLazyDataModel(){
        $ = new CollaborateConfigListRequest();
        lazyDataModel = new CollaborateConfigListLazyDataModel($);
    }

    public String add(){
        return redirect("/views/collaborate/collaborate-config-info.xhtml");
    }


    public void onRest(){
        initLazyDataModel();
    }

    public void remove(Integer id){
        if(null == id){
            addWarnMessage("无法找到id");
            return;
        }

        NewCollaborateChannelUser deleteInfo = collaborateChannelUserService.findOne(id);
        if(Objects.nonNull(deleteInfo)){
            collaborateChannelUserService.delete(id);
        }

        initLazyDataModel();
        addSuccessfullyMessage();
    }

    public String toInfo(Integer id){
        return redirect("/views/collaborate/collaborate-config-info.xhtml?id=" + id);
    }

    public String tranTypeStr(String typeStr){
        if(StringUtils.isNotBlank(typeStr)){
            String[] split = typeStr.split(",");
            StringBuilder builder = new StringBuilder();
            for (String aSplit : split) {
                CollaborateColShowEnum anEnum = CollaborateColShowEnum.getEnum(Integer.valueOf(aSplit));
                if(null != anEnum){
                    builder.append(anEnum.getDesc()).append(",");
                }
            }

            return builder.toString();
        }
        return typeStr;
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(DictionaryCode.COLLABORATE_TABLE_SHOW_TYPE,DictionaryCode.PARENT_PRODUCT);
    }

}
